/*
 * User: user
 * Date: 03/03/2008
 * Time: 13:01:59
 */
package org.conann.util;

import org.conann.exceptions.WebBeansResolutionException;

import java.util.Collection;

public class ValidationUtil {

    public static void checkHasOneElement(Collection<?> collection, String collectionName) {
        if (collection.isEmpty()) {
            throw new WebBeansResolutionException(collectionName + " is empty. It must have exactly 1 element.");
        }
        if (collection.size() != 1) {
            final StringBuilder sb = new StringBuilder(collectionName);
            sb.append(" has ").append(collection.size()).append(" elements. It must have exactly 1 element.\nElements are:\n");
            for (Object value : collection) {
                sb.append(value);
                sb.append(", ");
            }
            throw new WebBeansResolutionException(sb.toString());
        }
    }
}